package com.action;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Map;

import com.model.Registermodel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

public class login extends ActionSupport


{
	
	
	private String ebayuserid;
	private String password;
	
	public String getEbayuserid() {
		return ebayuserid;
	}

	public void setEbayuserid(String ebayuserid) {
		this.ebayuserid = ebayuserid;
	}
	  
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String str;
	
	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	
	ArrayList<String> category=new ArrayList<String>();
	ArrayList<String> subcategory=new ArrayList<String>();
	
	public ArrayList<String> getSubcategory() {
		return subcategory;
	}

	public void setSubcategory(ArrayList<String> subcategory) {
		this.subcategory = subcategory;
	}

	public ArrayList<String> getCategory() {
		return category;
	}

	public void setCategory(ArrayList<String> category) {
		this.category = category;
	}
	
	
	ResultSet res,res1,res2;
	
	public String execute()throws Exception
	
	{
		
	
	
		 Map session;
		 session=ActionContext.getContext().getSession();
    	 Integer checklogin=(Integer)session.get("checklogin");
		 System.out.println("The checklogin value is"+checklogin);
    	 
    	 
    	 String query="select * from register where ebayuserid='"+ebayuserid+"'and password='"+password+"';";
	     System.out.println("Query is"+query);	  
	 	 
	     res= DB.readFromBmtcDB(query);
	     System.out.println("Res is"+res);
	    
	    
	    
	       while(res.next())
	         {
	    	    
	    	   Integer active=res.getInt("active");   // To check if the account has been activated or not
	    	   if(active==1)
	    		   
	     	     {
	  	           
	    		   
	    		   System.out.println("Inside resultset");
	    	 
	    	 
	       	   String ebayuserid=res.getString("ebayuserid");
	    	   String password=res.getString("password");
	    	   Integer id=res.getInt("id");
	    	 
	    	 
	    	   session.put("ebayuserid",ebayuserid);
	    	   session.put("password",password);
	    	   session.put("id",id);
	    	 
	    	
	    	   
	    	   // For retrieving the details fro item location after login as a seller
	    	   
	    	   String query2="select * from register where id="+id+";";
	    	   res2=DB.readFromBmtcDB(query2);
	    	   while(res2.next())
	    	   {
	    		     session.put("homeaddress",res2.getString("homeaddress"));
	    			 session.put("city",res2.getString("city"));
	    			 session.put("state",res2.getString("state"));
	    			 
	    	   }
	          
	    	   
	    	   
	    	   // For the Category
	    	 
	    	String catname;
	    	String query1="select * from category;";
	   		res1=DB.readFromBmtcDB(query1);
	   		
	   		while(res1.next())
	   		{
	   			
	   		   catname=res1.getString("catname");
	   		   category.add(catname);
	   		   
	   		}
	   		
	   		System.out.println("The categories are"+category);
	    	   
	   		   if(checklogin==null)
	   			   return "login";
	    	   if(checklogin==1)
	    		   return "orderconfirmation_buyitnow";
	    	   else if(checklogin==2)
	    		   return "addtocart";
	    	   else if(checklogin==3)
	    		   return "confirmbid";
	    	   else 
		       return "login";
		
	            }
	           
	    	   str="Your Account is yet not activated.Kindly visit your email..!!!";
	    	   return "notactivated";
	          }    
	     
	     
	    	str="Incorrect username or Password..!!";
	     return "error";
	}
}
